﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using EndomondoWebAccess;
using OpenQA.Selenium.Firefox;
using SuuntoAmbitConnector;

namespace EndomondoWebAccessTests
{
    [TestClass]
    public class EndomondoWebDriverTests
    {
        private EndomondoWebDriver endomondoDriver;

        private const string ENDOMONDOUSERNAME = "suunto2endomondo@outlook.com";
        private const string ENDOMONDOPASSWORD = "suunto2endomondo";

        [TestInitialize]
        public void TestInitialize()
        {
            endomondoDriver = EndomondoWebDriver.Create(EndomondoWebDriver.WebDriverType.Firefox);
        }

        [TestCleanup]
        public void TestCleanUp()
        {
            if (endomondoDriver != null)
                endomondoDriver.Close();
        }




        [TestMethod]
        public void TestCreatingWebDriver()
        {
            Assert.AreEqual(typeof(FirefoxDriver), endomondoDriver.GetType(), "Unexpected Selenium Web Driver has been generated");            
        }

        [TestMethod]
        public void TestOpeningEndomondoWebSite()
        {
            //driver.Navigate().GoToUrl("http://www.endomondo.com/access");
            endomondoDriver.OpenWebSite();

            // Verfy login button exists
            Assert.IsTrue(endomondoDriver.IsWebSiteOpen());
        }

        [TestMethod]
        public void TestLoginToEndomondo()
        {
            endomondoDriver.Login(ENDOMONDOUSERNAME, ENDOMONDOPASSWORD);

            Assert.IsTrue(endomondoDriver.IsLoggedIn());
        }

        [TestMethod]
        public void TestOpenManualWorkoutSite()
        {
            endomondoDriver.Login(ENDOMONDOUSERNAME, ENDOMONDOPASSWORD);

            endomondoDriver.NavigateToManualWorkoutSite();

            Assert.IsTrue(endomondoDriver.IsManualWorkoutSiteOpen());            
        }

        [TestMethod]
        public void TestCreatingManualWorkoutWithMinimumData()
        {
            endomondoDriver.Login(ENDOMONDOUSERNAME, ENDOMONDOPASSWORD);
            endomondoDriver.NavigateToManualWorkoutSite();
            Workout expectedWorkout = new Workout(new DateTime(2013,8,22, 12, 40, 0), 3, 3000, "Created by Suunto2Endomondo");

            endomondoDriver.CreateManualWorkout(expectedWorkout);

            Assert.IsTrue(endomondoDriver.IsWorkoutCreated(expectedWorkout));
        }

        [TestMethod]
        public void TestCreatingManualWorkoutWithExtendedData()
        {
            endomondoDriver.Login(ENDOMONDOUSERNAME, ENDOMONDOPASSWORD);
            endomondoDriver.NavigateToManualWorkoutSite();
            Workout expectedWorkout = new Workout(new DateTime(2013, 8, 22, 12, 40, 0), 3, 3000, "Created by Suunto2Endomondo");
            expectedWorkout.DistanceInMeters = 5323;

            endomondoDriver.CreateManualWorkout(expectedWorkout);

            Assert.IsTrue(endomondoDriver.IsWorkoutCreated(expectedWorkout));
        }

        [TestMethod]
        [ExpectedException(typeof(NoSuchWindowException))]
        public void TestCreatingManualWorkoutFailsIfManualWorkoutPageIsNotOpened()
        {
            Workout expectedWorkout = new Workout(new DateTime(2013,8,22), 3, 3000, string.Empty);

            endomondoDriver.CreateManualWorkout(expectedWorkout);
        }   
     

        /*
         * E2E scenarios
         */

        [TestMethod]
        public void TestCreateManualWorkoutScenario()
        {
            Workout expectedWorkout = new Workout(new DateTime(2013, 8, 22, 12, 40, 0), 3, 3000, "Created by Suunto2Endomondo");
            expectedWorkout.DistanceInMeters = 5323;

            endomondoDriver.CreateManualWorkout(ENDOMONDOUSERNAME, ENDOMONDOPASSWORD, expectedWorkout);

            Assert.IsTrue(endomondoDriver.IsWorkoutCreated(expectedWorkout));
        } 
    }
}
